﻿/**
 * bio项目公用类库@ jquery-1.7.1.min.js
 * 2012年5月1日
 * Author 文光临 QQ：81780854
 * 
 */
;(function($) {
	$.fn.extend({
		getObjType : function(obj) {
			switch (obj) {
			case null:
				return "null";
			case undefined:
				return "undefined";
			}
			var s = Object.prototype.toString.call(obj);
			switch (s) {
			case "[object String]":
				return "string";
			case "[object Number]":
				return "number";
			case "[object Boolean]":
				return "boolean";
			case "[object Array]":
				return "array";
			case "[object Date]":
				return "date";
			case "[object Function]":
				return "function";
			case "[object RegExp]":
				return "regExp";
			case "[object Object]":
				return "object";
			default:
				return "object";
			}
		},
		uuId: function(len, radix) {//长度,进制
				var CHARS = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');
				var chars = CHARS, uuid = [], i;
				radix = radix || chars.length;
				if (len) {
					for (i = 0; i < len; i++)
						uuid[i] = chars[0 | Math.random() * radix];
				} else { // rfc4122, version 4 form     
					var r; // rfc4122 requires these characters  
					uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';
					uuid[14] = '4';
					for (i = 0; i < 36; i++) {
						if (!uuid[i]) {
							r = 0 | Math.random() * 16;
							uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r];
						}
					}
				}
				return uuid.join('');
		},
		dateFormat:function(date,format){
			var o = { 
				"M+" : date.getMonth()+1, //month 
				"d+" : date.getDate(), //day 
				"h+" : date.getHours(), //hour 
				"m+" : date.getMinutes(), //minute 
				"s+" : date.getSeconds(), //second 
				"q+" : Math.floor((date.getMonth()+3)/3), //quarter 
				"S" : date.getMilliseconds() //millisecond 
			} 
			if(/(y+)/.test(format)) { 
				format = format.replace(RegExp.$1, (date.getFullYear()+"").substr(4 - RegExp.$1.length)); 
			} 
			for(var k in o) { 
				if(new RegExp("("+ k +")").test(format)) { 
					format = format.replace(RegExp.$1, RegExp.$1.length==1 ? o[k] : ("00"+ o[k]).substr((""+ o[k]).length)); 
				} 
			} 
			return format; 
		},
		fillDateInBaseTag : function(jsonDate) {
			var out=this;
			if (this.getObjType(jsonDate) == 'object') {
				for (var key in jsonDate) {
					var $keyobj = this.find("#" + key);
					if ($keyobj.length == 1) {
						if(out.getObjType(jsonDate[key])=='array'){
							var inJsonDate=jsonDate[key];
							for (var inkey in inJsonDate) {
								out.bio_addRow(key,inJsonDate[inkey]);
							}
						}else{
							if($keyobj[0].nodeName=="SPAN"||$keyobj[0].nodeName=="LABEL"||$keyobj[0].nodeName=="FONT"){
								$keyobj.html(jsonDate[key]);
							}else{
								$keyobj.val(jsonDate[key]);
								if($keyobj.attr("defId")!=undefined && $keyobj.attr("defId")=="province"){
									$keyobj.change();
								}
							}
						}
					};
				}
			}
		},
		replaceImgSrc : function(addURL) {
			this.find("link").each(function(index, domEle) {
				$(domEle).attr({
					href : addURL + $(domEle).attr("href")
				});
			});	
			this.find("img").each(function(index, domEle) {
				$(domEle).attr({
					src : addURL + $(domEle).attr("src")
				});
			});			
		},
		setAllMessage : function() {//用时需要引入message_zh_CN.js
			this.find("[messageID]").each(function(index, domEle) {
				if(domEle.nodeName=='INPUT'){
					$(domEle).attr({value:bio_message[$(domEle).attr("messageID")]});					
				}else{					
					domEle.innerHTML=bio_message[$(domEle).attr("messageID")];
				};				
			});			
		},
		setSelectDef : function() {//用时需要引入message_zh_CN.js和selectResource.js
				var out=this;
				out.find("select[defId]").each(function(index, domEle) {
					var selDef=bio_selectDef[$(domEle).attr("defId")];
					//domEle.options.length = 0;
					if (out.getObjType(selDef)=="array"){
						$.each(selDef,function(i,item){
							domEle.options.add(new Option(item.name, item.value));
						});
					};
					if(out.getObjType(selDef)=="number"||out.getObjType(selDef)=="object"){
						var postUrl,postData,opName;
						if(out.getObjType(selDef)=="number"){
							postUrl="/codList/queryMyCodeL.action";
							postData={"parentId":selDef };
							opName="typeName";
						}
						if(out.getObjType(selDef)=="object"){
							postUrl=selDef.url;
							postData=selDef.parameter;
							opName=selDef.opName;
						}
						$.ajax({
			                url : out.getPath()+postUrl,
			                data:postData,
			                cache : false, 
			                async : false,
			                type : "POST",
			                dataType : 'json',
			                success : function(data){
									if(data!=null){
									$.each(data,function(i,item){										
										domEle.options.add(new Option(item[opName],item.id));										
									})};																    
							}
			            });										
					};
			});			
		},
		getPath : function() {//取得发布路径		
			var strFullpath= window.document.location.href;
			var strPath = window.document.location.pathname;
			var pos = strFullpath.indexOf(strPath);
			var prePath = strFullpath.substring(0, pos);
			var enName = strPath.substring(0, strPath.substr(1).indexOf('/') + 1);	
			return enName;
		},
		jumpUrl : function(url,jsonDate) {//页面间跳转 与传参
			if (url==""||url==null||url==undefined){
				return;
			}
			if(jsonDate==null||jsonDate==undefined){
				window.document.location.href=url;
			}//拼装参数
			url=this.getPath()+url+"?";
			for (var key in jsonDate) {
				url=url+key+"="+encodeURI(jsonDate[key])+"&";
			}
			if(jsonDate!=null&&jsonDate!=undefined){
				url=url.substring(0, url.length-1);
			}			
			window.document.location.href=url;
			return;
		},
		getParameter : function(key) {//取跳转后的参数
			//构造参数对象并初始化			 
			 var str=window.document.location.href.toString();  //获得浏览器地址栏URL串
			 var num=str.indexOf("?");
			 if (num==-1){
				 return null;
			 }
			 str=str.substr(num+1);  //截取?后面的参数串
			 num=str.indexOf(key);
			 if (num==-1){
				 return null;
			 }
			 str=str.substr(num);			 
			 var arrtmp=str.split("&"); //将各参数分离形成参数数组
			 return decodeURI(arrtmp[0].split("=")[1]);			
		},
		bio_addRow : function(toAddID,fillData) {//添加一行	
			var $temp=$("#add_"+toAddID+" ul").clone(true);
			if(fillData!=undefined){
				$temp.fillDateInBaseTag(fillData);
			};
			$temp.appendTo("#"+toAddID);
			return $temp;
		},
		bio_addRow4batch : function(AddID,toAddID,fillData) {//添加一行	
			var $temp=$("#"+AddID+" li").clone(true);
			if(fillData!=undefined){
				$temp.fillDateInBaseTag(fillData);
			};
			$temp.appendTo("#"+toAddID);
		},
		bio_delRow : function(delAddID) {//删除一行			
			$("#"+delAddID).find("input:checked").parent().parent().remove();
		},
		getAllMap:function(delAddID) {//删除一行			
			$("#"+delAddID).find("input:checked").parent().parent().remove();
		},
		change4tab:function(obj){//Tab页面切换,span的tab值和div的tab的值相同<样式不同>
			var name=obj.tab;//获取tab值
			//设置所有tab头为不选中样式,第一个tab样式不同
			$("span[tab='head']").attr("class","no-select2");
			$("span[tab^='other_']").attr("class","no-select");
			//隐藏所有tab页
			$("div[tab]").hide();
			//设置选中页样式
			$("span[tab='"+name+"']").attr("class","select");
			//显示选中tab页
			$("div[tab='"+name+"']").show();
		},
		fn4tab:function(key_id,iscls){//自定义属性tab 头与页面div的tab值相同
			//tab头第一个tab固定为head其他以other_开头命名
			$("span[tab]").each(function(index,domEle){
				$(domEle).click(function(){
					$(document).change4tab(domEle);
					$(document).checkDate(domEle,key_id,iscls,function(){});
				});
			});
		},
		checkDate:function(obj,key_id,iscls,callback){//单击tab页头时请求数据,iscls(是否删除,no不删除,yes删除)
			//acId=/donor/actionName,acId值书写方式,key_id为当前主表Id
			var isReq="true";//是否请求
			var isDel="yes";//是否删除action属性
			if(null!=iscls){
				isDel=iscls;
			}
			if(null!=obj.isReq){//如果不为空则替换其值
				isReq=obj.isReq;
			}
			var name=obj.tab;
			if(obj.acId!=null && obj.acId!=""){//如果请求ACID不为空再进行请求
				url=this.getPath()+obj.acId+".action";
				var id=$("#"+key_id).val();
				if(id!="" && isReq=="true"){//如果主信息不为空则查询附属信息
					$.post(url,
						  {"id":id},//捐赠者ID
						  function(data) {
						  	//回填数据
						  	var myaa=new Object();
						 	myaa[name]=data;
						  	$("#myforms").fillDateInBaseTag(myaa);//form的属性id:myforms
						  	if("yes"==isDel){
							  obj.acId="";
						  	}
						  	if(undefined!=callback){
						  		callback();
						  	}
						  },
						  "json"
					);
					$(obj).attr("isReq","false");
		   		}
			}
		},
		//弹出页面的地址,参数:okVal:确定按钮value,回调函数
		opDialogMin:function(path,dialogOptions,callback){//弹窗方法
			var dialogSet = {
				title : "请选择",
				lock : true,
				background : '#666666',// 背景色
				opacity : 0.5,// 透明度
				width : '600px',
				height : '400px',
				resize : false,
				drag : true,
				esc : true,
				padding : 0,
				ok : function() {
					var data=this.iframe.contentWindow.doAction();
					if(data!=false){
						if(callback!=undefined){
							callback(data);
						}
					}else{
						return false;
					}
				},
				//cancel:function(){
				//},
				okVal :"选   择",
				//cancelVal : "关   闭",
				//closeFn : function() {
				//},
				close:function(){
					if(callback!=undefined){
						callback();
					}
				}
			};
			$.extend(dialogSet, dialogOptions);
			//打开对话框
			var url=$().getPath() + path;
			var origin=$.dialog.open(url, dialogSet);
			art.dialog.data("origin",origin);
		}
	});
})(jQuery);
